package com.lykj.modules.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lykj.common.cache.ParamCache;
import com.lykj.modules.system.entity.Param;
import com.lykj.modules.system.entity.Tenant;
import com.lykj.modules.system.service.ParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import com.lykj.core.mp.support.Condition;
import com.lykj.core.mp.support.Query;
import com.lykj.core.tool.api.R;
import com.lykj.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lykj.modules.shop.entity.StoreMarket;
import com.lykj.modules.shop.vo.StoreMarketVO;
import com.lykj.modules.shop.wrapper.StoreMarketWrapper;
import com.lykj.modules.shop.service.StoreMarketService;
import com.lykj.core.boot.ctrl.LykjController;

/**
 * 贡献值行情表 控制器
 *
 * @author cxl
 * @since 2022-01-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("lykj-shop/store/market")
@Api(value = "贡献值行情表", tags = "贡献值行情表接口")
public class StoreMarketController extends LykjController {

	private final StoreMarketService storeMarketService;
	private final ParamService paramService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入storeMarket")
	public R<StoreMarketVO> detail(StoreMarket storeMarket) {
		StoreMarket detail = storeMarketService.getOne(Condition.getQueryWrapper(storeMarket));
		return R.data(StoreMarketWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 贡献值行情表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入storeMarket")
	public R<IPage<StoreMarketVO>> list(StoreMarket storeMarket, Query query) {
		IPage<StoreMarket> pages = storeMarketService.page(Condition.getPage(query.setDescs("create_time")), Condition.getQueryWrapper(storeMarket));
		return R.data(StoreMarketWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 贡献值行情表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入storeMarket")
	public R<IPage<StoreMarketVO>> page(StoreMarketVO storeMarket, Query query) {
		query.setDescs("id");
		IPage<StoreMarketVO> pages = storeMarketService.selectStoreMarketPage(Condition.getPage(query.setDescs("create_time")), storeMarket);
		return R.data(pages);
	}

	/**
	 * 新增 贡献值行情表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入storeMarket")
	public R save(@Valid @RequestBody StoreMarket storeMarket) {
		return R.status(storeMarketService.save(storeMarket));
	}

	/**
	 * 修改 贡献值行情表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入storeMarket")
	public R update(@Valid @RequestBody StoreMarket storeMarket) {
		return R.status(storeMarketService.updateById(storeMarket));
	}

	/**
	 * 新增或修改 贡献值行情表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入storeMarket")
	public R submit(@Valid @RequestBody StoreMarket storeMarket) {
		String contributeValue = paramService.getValue("contribute.value");
		if (Func.isNotEmpty(contributeValue)) {
			JSONObject jsonValue = JSONObject.parseObject(contributeValue);
			if (jsonValue instanceof JSONObject) {
				storeMarket.setBeforeValue(jsonValue.getBigDecimal("now"));
				storeMarket.setLaterValue(storeMarket.getBeforeValue().add(storeMarket.getChangeValue()));
				jsonValue.put("now", storeMarket.getLaterValue());
				paramService.update(Wrappers.<Param>update().lambda().set(Param::getParamValue, jsonValue.toJSONString()).eq(Param::getParamKey, "contribute.value"));
			}
		}
		return R.status(storeMarketService.saveOrUpdate(storeMarket));
	}


	/**
	 * 删除 贡献值行情表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(storeMarketService.deleteLogic(Func.toLongList(ids)));
	}


}
